package com.meichis.yndms.adapter.RecycleViewItemType;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.meichis.mcsappframework.adapter.recyclerview.base.ItemViewDelegate;
import com.meichis.mcsappframework.adapter.recyclerview.base.ViewHolder;
import com.meichis.yndms.R;
import com.meichis.yndms.component.CityChoicePopView;
import com.meichis.yndms.model.InspectTemplate_Item;
import com.meichis.yndms.service.LocationService;
import com.meichis.yndms.ui.activity.VST_FreeQuestionnaireLogicActivity;

import java.util.List;

/**
 * Created by user on 2018/3/28.
 */

public class FillMode_City implements ItemViewDelegate<InspectTemplate_Item>, View.OnClickListener{

    private final Activity activity;
    private List<InspectTemplate_Item> items;
    private LocationService locationService;
    private TextView textView;
    private Context context;
    private Intent intent;
    private boolean isFirst = true;
    private int position;

    public FillMode_City(Context context, Activity activity, List<InspectTemplate_Item> items) {
        this.items = items;
        this.context = context;
        this.activity = activity;

    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.fill_mode_city;
    }

    @Override
    public boolean isForViewType(InspectTemplate_Item item, int position) {
        return item.getItem().getFillMode() == 11;
    }

    @Override
    public void convert(ViewHolder holder, InspectTemplate_Item inspectTemplate_item, int position) {
        this.position = position;
        textView = holder.getView(R.id.tv_template_city);
        holder.setText(R.id.tv_question, Html.fromHtml(String.valueOf(position + 1) + ". " +
                inspectTemplate_item.getItem().getName() +
                (!TextUtils.isEmpty(inspectTemplate_item.getItem().getUnit()) ? "(" + inspectTemplate_item.getItem().getUnit() + ")" : "") +
                (inspectTemplate_item.getIsMustFill() == 1 ? " <font  color=\"#FF0000\">*</font>" : "") +
                (!TextUtils.isEmpty(inspectTemplate_item.getItem().getSubTitle()) ? "<br/> <font color=\"#8D8D8D\" ><small>" + inspectTemplate_item.getItem().getSubTitle() + "</small></font>" : "")
        ));
        if(TextUtils.isEmpty(inspectTemplate_item.getFillName())){
            bindLocationServices();
        }else{
            textView.setText(inspectTemplate_item.getFillName());
            //会导致IllegalArgumentException 错误，xmy-2018-05-12在此屏蔽
//            context.unbindService(conn);
            locationService = null;
        }

        textView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId())
        {
            case R.id.tv_template_city:
                new CityChoicePopView(context, 0, new CityChoicePopView.SelectedListener() {
                    @Override
                    public void setValue(String Name, int ID) {
                        textView.setText(Name);
                        items.get(position).setFillName(Name.toString());
                        items.get(position).setFillValue(Name.toString());
                    }
                }).showAtLocation(textView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 1);
                break;
            default:
                break;
        }
    }

    private void bindLocationServices() {
        intent = new Intent(context, LocationService.class);
        context.bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            locationService = ((LocationService.LocalBinder) service).getService();
            if (locationService != null) {
                locationService.getLocation(new AMapLocationListener() {
                    @Override
                    public void onLocationChanged(AMapLocation location) {
                        if(isFirst)
                        {
                            textView.setText(location.getProvince()+" "+location.getCity()+" "+location.getDistrict());
                            items.get(position).setFillName(location.getProvince()+" "+location.getCity()+" "+location.getDistrict());
                            items.get(position).setFillValue(location.getProvince()+" "+location.getCity()+" "+location.getDistrict());
                            isFirst = false;
                            if (locationService != null) {
                                context.unbindService(conn);
                                locationService = null;
                            }
                        }
                    }
                });
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            locationService = null;
        }

    };
}
